iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 16
0

導言

本次範例以銀行功能為目標 (( 快沒靈感了...

程式碼

pragma solidity^0.4.25;
library SafeMath{
    function add(uint a, uint b) internal pure returns (uint c) {
        c = a + b;
        require(c >= a);
    }
    function sub(uint a, uint b) internal pure returns (uint c) {
        require(b <= a);
        c = a - b;
    }
    function mul(uint a, uint b) internal pure returns (uint c) {
        c = a * b;
        require(a == 0 || c / a == b);
    }
    function div(uint a, uint b) internal pure returns (uint c) {
        require(b > 0);
        c = a / b;
    }
}

contract Bank{
    using SafeMath for uint;
    uint public BankDeposit;
    constructor () public payable{
        BankDeposit = msg.value/10**18; //以ether為1單位
    }
    
    mapping (address => uint) deposits;
    mapping (address => mapping(address=>uint)) allowed;

    event Deposit(address indexed _sender, uint _value , uint _timstamp);
    event Approval(address indexed _owner, address indexed _spender, uint _value , uint timstamp);
    event Transfer(address indexed _from, address indexed _to, uint _value , uint timstamp);
    
    //存款
    function deposit() public payable returns(bool){
        require(msg.value > 0 );
        deposits[msg.sender] = msg.value.div(10**18);
        BankDeposit.add(msg.value);
        emit Deposit(msg.sender,deposits[msg.sender],now);
        return true;
    }
    
    //提款
    function withdraw(uint _value) public {
        require(deposit[msg.sender] > _value);
        msg.sender.transfer(_value);
        deposits[msg.sender].sub(_value);
        emit Transfer(address(this),msg.sender,_value);
        return true;
    }

    //轉帳
    function approve(address _spender, uint _value)) public returns(bool){
        require(_value > 0);
        require(deposits[msg.sender] > _value);
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender,_spender,_value);
        return true;
    }

    //執行轉帳
    function transferFrom(address _from, address _to, uint _value) public returns(bool){
        require(deposits[_from] > _value);
        require(allowed[_from][_to] >= _value);
        allowed[_from][_to].sub(_value);
        deposits[_from].sub(_value);
        deposits[_to].add(_value);
        emit Transfer(_from,_to,_value);
        return true;

    }
    
    //查詢他人轉帳給本人的額度
    function allowance(address _owner) public view returns(uint256){
        return allowed[_owner][msg.sender];
    }

    //查詢存款
    function depsoition() public view returns(uint256){
        return deposits[msg.sender];
    }

}

解說

(待補)


上一篇
Day14-DailyRent
下一篇
Day16-ERC721
系列文
30天30個Smart contract 20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言